/*
 * CommonUtil
 * 
 * Version 1.2
 *
 * 31-07-08
 * 
 * Modification Logs:
 *   DATE        AUTHOR      DESCRIPTION
 *  --------------------------------------------------------
 *  31-07-08	TuHA		declare class prototype
 *  12-08-08	TuHA		create sendMail method
 *  19-08-08	TuHA		fix mail host variable, comment file.
 */

package vn.fpt.fsoft.fcos.dao;

import java.sql.Connection;
import java.util.Date;
import java.util.HashMap;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;

import vn.fpt.fsoft.fcos.entity.OrderEntity;

/**
 * This class contains all of common util methods
 * CommonUtil
 * @author Hoang Anh Tu
 * @version 1.0 31-07-08
 */
public class CommonUtil {
	
	/**
	 * Ham sendMail
	 * @param order: an OrderEntity of supplier
	 * @param recipients: a string of recipients
	 * @return: true when email is sent successfully.
	 * 			false when sending unsuccessfully.
	 */
	public boolean sendEmail(OrderEntity order, String recipients){
		
		// init host 
		String host = "mail.fsoft.com.vn";
		
		// init from email
		String from = "tuha@fsoft.com.vn";
		
		// init to email
		String to = "kenji251143@gmail.com";
		
		// init email subject
		String subject = "Hello mail";
		
		// init email content 
		String content = "This is hello mail form FCOS";
		
		// init properties object
		Properties props = new Properties();
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.port", "465");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "true");
		
		try{
			
			// create session object
			Session session = Session.getDefaultInstance(props, new Authenticator(){
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication("kenji25115@gmail.com", "iloveyou251143");
				}
			}); 
			
			// create message object
			Message msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(from));
			msg.setRecipient(RecipientType.TO, new InternetAddress(to));
			msg.setContent(content, "text/plain");
			msg.setSubject(subject);
				
			// send email
			Transport.send(msg);
					
		}catch(Exception exp){
			
			// when an exception is generated, we return false
			return false;
		}
	    
		// when email is sent successfully
		return true;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		CommonUtil util = new CommonUtil();
		
	//	util.sendEmail();
	}
}
